package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ISetmealService;
import com.itheima.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 
 * @since 2024-05-27
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    /**
     * 显示购物车信息
     */
    @Autowired
    public IShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> showList(){
        Long userId = BaseContext.get();
        List<ShoppingCart> shoppingCartList = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .list();
        return R.success(shoppingCartList);
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info(shoppingCart.toString());
        //1.设置用户id 指定当前操作是哪个用户的购物车数据
        Long id = BaseContext.get();
        shoppingCart.setUserId(id);
        //2.查询当前用户id下是否存在此次添加的购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //2.1判断本次添加的购物车是添加菜品还是套餐用以构建查询套件
        if(shoppingCart.getDishId() ==null){
            //2.2说明此次添加的是套餐类的购物车,查询套件判断是否已存在当前用户下相同套餐id的记录
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {
            //2.3说明此次添加的是菜品类的购物车,查询套件判断是否已存在当前用户下相同菜品id的记录
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart shoppingCartInfo = shoppingCartService.getOne(wrapper);
        //3.如果查询到的购物车记录已存在,则属性'number'加一,不存在则保持当前购物车记录到表中
        if(shoppingCartInfo!=null){
            //3.1已存在 更新'number'字段
            Integer number = shoppingCartInfo.getNumber();
            shoppingCartInfo.setNumber(number+1);
            shoppingCartService.updateById(shoppingCartInfo);
            return R.success(shoppingCartInfo);
        }
        //3.2不存在
        /**
         * 注意:
         * 虽然新增存入数据库记录的number属性默认值是1,但是对象添加到数据库时,属性number值并不会回显到对象中,
         * 因而返回该对象时,对象内的number属性还是null值,手动设置number值即可传递给前端正确number属性
         */
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = BaseContext.get();
        shoppingCartService.lambdaUpdate()
                .eq(ShoppingCart::getUserId,userId)
                .remove();
        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R<ShoppingCart> reduceOnce(@RequestBody ShoppingCart shoppingCart){
        return shoppingCartService.reduceOnce(shoppingCart);
    }
}
